﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;
using System.IO;

namespace GUI
{
    public partial class frmGiaoVien : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo bien
        public string maDocGia = "";
        DataTable dt;
        string maDanToc = "";
        string maChucVu = "";
        string maDonVi = "";
        string maLoaiThe = "";
        TextMessageBox box = new TextMessageBox();
        DocGia_BLL dg_bll = new DocGia_BLL();
        GiaoVien_BLL gv_bll = new GiaoVien_BLL();
        Image image;
        string path = "";
        private bool isOk = false;
        private bool checkTT = false;
        DateTime ngayLamThe;
        private int maThamSo ;
        #endregion

        #region LoadForm

        public frmGiaoVien()
        {
            InitializeComponent();
        }

        private void frmGiaoVien_Load(object sender, EventArgs e)
        {
            
            LoadCombobox();
            LoadData();

        }

        #endregion

        #region cac chuc nang

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            maChucVu = "";
            maDanToc = "";
            maDonVi = "";
            maDocGia = "";
            txtMaGiaoVien.Text = "Mã tự sinh";
            txtHoTen.Focus();
            pbAnh.Image = null;
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void btnAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "(JPG fie)|*.jpg|*.jpg|(All file)|*.*|*.*";
            op.FilterIndex = 1;
            op.RestoreDirectory = true;
            if (op.ShowDialog() == DialogResult.OK)
            {
                path = op.FileName;
                pbAnh.ImageLocation = op.FileName;
            }
        }
        private void cboDanToc_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maDanToc = cboDanToc.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboChucVu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maChucVu = cboChucVu.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboDonVi_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maDonVi = cboDonVi.EditValue.ToString();
            }
            catch(Exception)
            {

            }
        }

        private void cboLoaiThe_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maLoaiThe = cboLoaiThe.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboThamSo_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maThamSo =box.ToInt(cboThamSo.EditValue.ToString());
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Hamf vieet them
        private void UpdateData()
        {
            if (checkValid())
            {
                DocGia_Public dg = new DocGia_Public();
                GiaoVien_Public gv = new GiaoVien_Public();
                if (maDocGia == "")
                {
                    if (checkMaInsert())
                    {
                        string ma = "";
                        //luu vao bang Doc  Gia
                        ma = box.AutoID("DG", 2, gv_bll.TaoBang("select * from DocGia"));
                        dg.MaDocGia = ma;
                        dg.NgayLamThe = DateTime.Now;
                        dg.NgayHetHan = dtpNgayHetHan.Value;
                        dg.MaLoaiThe = maLoaiThe;
                        dg.TrangThaiSD = checkTT;
                        dg.MaNhanVien = box.MaNhanVien;

                        dg_bll.Insert(dg);
                        //lưu vào bảng CTDG_Giao Vien
                        gv.MaDocGia = ma;
                        gv.Anh = converToByte(path);
                        gv.HoVaTen =box.toUpper(txtHoTen.Text);
                        gv.GioiTinh =box.toUpper(cboGioiTinh.Text);
                        gv.NgaySinh = dtpNgaySinh.Value;
                        gv.MaDanToc = maDanToc;
                        gv.TrinhDo = txtTrinhDo.Text;
                        gv.MaChucVu = maChucVu;
                        gv.MaDonVi = maDonVi;
                        gv.MaThamSo = maThamSo;
                        gv.SoDienThoai = txtDienThoai.Text;
                        gv.Email = txtEmail.Text;

                        gv_bll.Insert(gv, gv, gv, gv, gv, gv, gv, gv, gv, gv, gv,gv);
                        CloseForm();
                    }
                }
                else
                {
                    //Update bang Doc gia
                    dg.MaDocGia = maDocGia;
                    dg.NgayLamThe = ngayLamThe;
                    dg.NgayHetHan = dtpNgayHetHan.Value;
                    dg.MaLoaiThe = maLoaiThe;
                    dg.TrangThaiSD = checkTT;
                    dg.MaNhanVien = box.MaNhanVien;

                    dg_bll.Update(dg);
                    //update vao bang CTDG Giao Vien
                    gv.MaDocGia =maDocGia;
                    gv.Anh = converToByte(path);
                    gv.HoVaTen =box.toUpper(txtHoTen.Text);
                    gv.GioiTinh = cboGioiTinh.Text;
                    gv.NgaySinh = dtpNgaySinh.Value;
                    gv.MaDanToc = maDanToc;
                    gv.TrinhDo = txtTrinhDo.Text;
                    gv.MaChucVu = maChucVu;
                    gv.MaDonVi = maDonVi;
                    gv.MaThamSo = maThamSo;
                    gv.SoDienThoai = txtDienThoai.Text;
                    gv.Email = txtEmail.Text;

                    gv_bll.Update(gv, gv, gv, gv, gv, gv, gv, gv, gv, gv, gv,gv);
                    CloseForm();
                }
            }
        }
        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
            {
                box.Huy_Text(this);
                SetInterface();
            }
        }
        private void LoadData()
        {
            if (maDocGia != "")
            {
                dt = gv_bll.Load_GiaoVien_Edit(maDocGia);
                DataRow dr = dt.Rows[0];
                txtMaGiaoVien.Text = maDocGia;
                txtHoTen.Text=dr["HoVaTen"].ToString();
                cboGioiTinh.Text=dr["GioiTinh"].ToString();
                dtpNgaySinh.Text=dr["NgaySinh"].ToString();
                maDanToc=dr["MaDanToc"].ToString();
                txtTrinhDo.Text=dr["TrinhDo"].ToString();
                maChucVu=dr["MaChucVu"].ToString();
                maDonVi = dr["MaDonVi"].ToString();
                txtDienThoai.Text = dr["SoDienThoai"].ToString();
                txtEmail.Text=dr["Email"].ToString();
                maLoaiThe = dr["MaLoaiThe"].ToString();
                txtMaGiaoVien.Enabled = false;
                pbAnh.Image = ConvertToImage(dt);
                ngayLamThe=Convert.ToDateTime(dr["NgayLamThe"]);
                dtpNgayHetHan.Text = dr["NgayHetHan"].ToString();
                maThamSo = box.ToInt(dr["MaThamSo"]);
                if (Convert.ToBoolean(dr["TrangThaiSD"]) == true)
                    ckbTrangThaiSD.CheckState = CheckState.Checked;
                else
                    ckbTrangThaiSD.CheckState = CheckState.Unchecked;
                //
                cboChucVu.EditValue = dr["MaChucVu"].ToString();
                cboDanToc.EditValue = dr["MaDanToc"].ToString();
                cboDonVi.EditValue = dr["MaDonVi"].ToString();
                cboLoaiThe.EditValue = dr["MaLoaiThe"].ToString();

            }
        }
        private byte[] converToByte(string url)
        {
            byte[] picbyte = null;
            FileInfo info = new FileInfo(url);
            long numByte = info.Length;
            FileStream fs;
            fs = new FileStream(url, FileMode.Open, FileAccess.Read);

            //fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
            //fs.Close();
            BinaryReader br = new BinaryReader(fs);
            picbyte = br.ReadBytes((int)numByte);
            return picbyte;

        }
        private Image ConvertToImage(DataTable d)
        {
            try
            {

                byte[] stream = (byte[])d.Rows[0][1];

                using (MemoryStream ms = new MemoryStream(stream, 0, stream.Length))
                {
                    ms.Write(stream, 0, stream.Length);
                    image = Image.FromStream(ms, true);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return image;
        }
        private void SetInterface()
        {
            maDocGia = "";
            maChucVu = "";
            maDanToc = "";
            maDonVi = "";
            path = "";
            txtMaGiaoVien.Enabled = true; 
        }
        private void LoadCombobox()
        {
            cboDanToc.Properties.DataSource = gv_bll.TaoBang(box.sqlSelectFile("MaDanToc", "TenDanToc", "DM_DanToc"));
            cboDanToc.Properties.DisplayMember = "TenDanToc";
            cboDanToc.Properties.ValueMember = "MaDanToc";

            cboChucVu.Properties.DataSource = gv_bll.TaoBang(box.sqlSelectFile("MaChucVu","TenChucVu","DM_ChucVu"));
            cboChucVu.Properties.DisplayMember = "TenChucVu";
            cboChucVu.Properties.ValueMember = "MaChucVu";

            cboDonVi.Properties.DataSource = gv_bll.TaoBang(box.sqlSelectFile("MaDonVi","TenDonVi","DM_DonVi"));
            cboDonVi.Properties.DisplayMember = "TenDonVi";
            cboDonVi.Properties.ValueMember = "MaDonVi";

            cboLoaiThe.Properties.DataSource = gv_bll.TaoBang(box.sqlSelectFile("MaLoaiThe","TenLoaiThe","DM_LoaiThe"));
            cboLoaiThe.Properties.DisplayMember = "TenLoaiThe";
            cboLoaiThe.Properties.ValueMember = "MaLoaiThe";

            cboThamSo.Properties.DataSource = gv_bll.TaoBang(box.sqlSelectFile("MaThamSo","TenThamSo","ThamSo"));
            cboThamSo.Properties.DisplayMember = "TenThamSo";
            cboThamSo.Properties.ValueMember = "MaThamSo";
        }

        private bool checkValid()
        {
            if (txtMaGiaoVien.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Mã giáo viên.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtMaGiaoVien.Focus();
                return false;
            }
            if (txtHoTen.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Họ tên giáo viên.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtHoTen.Focus();
                return false;
            }
            if (cboGioiTinh.Text == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Giới tính.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboGioiTinh.Focus();
                return false;
            }
            if (box.CompareDate(dtpNgaySinh.Value, DateTime.Now) == 1)
            {
                XtraMessageBox.Show("Ngày sinh không được lớn hơn ngày hiện tại.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                dtpNgaySinh.Focus();
                return false;
            }
            if (cboDanToc.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Dân tộc.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboDanToc.Focus();
                return false;
            }
            if (txtTrinhDo.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Trình độ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTrinhDo.Focus();
                return false;
            }
            if (cboChucVu.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Chức vụ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboChucVu.Focus();
                return false;
            }
            if (cboDonVi.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Đơn vị.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboDonVi.Focus();
                return false;
            }
            if (path == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Ảnh.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                btnAnh.Focus();
                return false;
            }
            if (box.CompareDate(DateTime.Now,dtpNgayHetHan.Value) == 1)
            {
                XtraMessageBox.Show("Ngày hết hạn không được lớn hơn ngày hiện tại.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                dtpNgaySinh.Focus();
                return false;
            }
            return true;
        }

        private bool checkMaInsert()
        {
            //dt = gv_bll.TaoBang(box.sqlCheckExist("CTDG_GiaoVien","MaDocGia",txtMaGiaoVien.Text.Trim()));
            //if (dt.Rows.Count > 0)
            //{
            //    XtraMessageBox.Show(box.Exist(txtMaGiaoVien.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
            //    txtMaGiaoVien.Focus();
                //return false;
            //}
            return true;
        }
        #endregion 


        #region Các sự kiện
        private void frmGiaoVien_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        private void ckbTrangThaiSD_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTrangThaiSD.CheckState == CheckState.Checked)
                checkTT = true;
            else
                checkTT = false;
        }
        #endregion








    }
}